
#include "stdafx.h"
#include "../client/DCPlusPlus.h"
#include "Resource.h"

#include "UpdatePage.h"
#include "../client/File.h"
#include "../client/SettingsManager.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

PropPage::Item UpdatePage::items[] = {
	{ IDC_CLIENT_ADDRESS,				SettingsManager::UPDATE_URL,			PropPage::T_STR_SHOWDEFAULT },
	{ IDC_MYINFO_ADDRESS,				SettingsManager::UPDATE_MYINFOS,		PropPage::T_STR_SHOWDEFAULT },
	{ IDC_CLIENTVERSION_ADDRESS,		SettingsManager::CLIENT_VERSION_SITE,	PropPage::T_STR_SHOWDEFAULT },
	{ IDC_ISP_ADDRESS,					SettingsManager::UPDATE_ISP_URL,		PropPage::T_STR_SHOWDEFAULT },
	{ 0, 0, PropPage::T_END }
};

LRESULT UpdatePage::onInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) {
	UpdateManager::getInstance()->addListener(this);
	PropPage::read((HWND)*this, items);
	progress.Attach(GetDlgItem(IDC_PROGRESS));
	return TRUE;
}

void UpdatePage::write() {
	PropPage::write((HWND)*this, items);
	UpdateManager::getInstance()->removeListener(this);
}

LRESULT UpdatePage::onBtn(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	prog = 0;
	uc = 0;
	getActivated();
	if(updateItems.size() == 0){
		::EnableWindow(GetDlgItem(IDC_UPDATE), true);
		::EnableWindow(GetDlgItem(IDC_LOAD_BACKUP), true);
		SetDlgItemText(IDC_STATUS, _T("Failed, You didn't choose any files."));
		return 0;
	} else {
		if(MessageBox(_T("This will replace your current profile settings, do you wish to proceed?."), _T("Warning!"), MB_YESNO|MB_ICONWARNING) != IDYES) {
			return 0;
		}
	}
	SetDlgItemText(IDC_STATUS,_T(""));
	progress.SetPos(0);
	::EnableWindow(GetDlgItem(IDC_UPDATE), false);
	::EnableWindow(GetDlgItem(IDC_LOAD_BACKUP), false);
	
	string statText;
	if(wID == IDC_UPDATE)		
		statText= "Downloading " + Util::toString(uc)+" files...\r\n";
	else if(wID == IDC_LOAD_BACKUP)	
		statText = "Reloading " + Util::toString(uc)+" files...\r\n";
	
	SetDlgItemText(IDC_STATUS, statText.c_str());
	progress.SetRange(0, uc);

	if(wID == IDC_UPDATE)		
		UpdateManager::getInstance()->updateFiles(updateItems);
	else if(wID == IDC_LOAD_BACKUP){
		string statText = "Reloading " + Util::toString(uc)+" files...\r\n";
		SetDlgItemText(IDC_STATUS, statText.c_str());
		for(StringIntIter j = updateItems.begin(); j != updateItems.end(); ++j) {
			try {
				string fname = Util::getAppPath() + "XML\\" + Util::getUpdateFileNames((int)j->first);
				File::renameFile(fname, fname + ".tmp");
				try {
					File::renameFile(fname + ".old", fname);
				} catch (...) {
					File::renameFile(fname + ".tmp", fname);
					continue;
				}
				File::renameFile(fname + ".tmp", fname + ".old");
			} catch(...) {
				continue;
			}
			
			progress.SetRange(0, uc);
			UpdateManager::getInstance()->reloadFile((int)j->first);
		}
		::EnableWindow(GetDlgItem(IDC_UPDATE), true);
		::EnableWindow(GetDlgItem(IDC_LOAD_BACKUP), true);
		uncheck(IDC_CLIENT_ACTIVE);
		uncheck(IDC_MYINFO_ACTIVE);
		uncheck(IDC_CLIENTVERSION_ACTIVE);
		uncheck(IDC_ISP_ACTIVE);
	}

	return 0;
}

string UpdatePage::getText(int id) {
	uc++;
	TCHAR buf[512];
	GetDlgItemText(id, buf, 512);
	tstring text = buf;
	if(Util::stricmp(text.substr(text.size()-1, 1), "\\") != 0 && Util::stricmp(text.substr(text.size()-1, 1), "/") != 0)
		text += PATH_SEPARATOR;
	return text;
}

void UpdatePage::getActivated() {
	updateItems.clear();
	if(IsDlgButtonChecked(IDC_CLIENT_ACTIVE) == BST_CHECKED)
		updateItems.insert(make_pair(UpdateManager::CLIENT,getText(IDC_CLIENT_ADDRESS)));
	if(IsDlgButtonChecked(IDC_MYINFO_ACTIVE) == BST_CHECKED)
		updateItems.insert(make_pair(UpdateManager::MYINFO,getText(IDC_MYINFO_ADDRESS)));
	if(IsDlgButtonChecked(IDC_CLIENTVERSION_ACTIVE) == BST_CHECKED)
		updateItems.insert(make_pair(UpdateManager::CLIENTVERSION,getText(IDC_CLIENTVERSION_ADDRESS)));
	if(IsDlgButtonChecked(IDC_ISP_ACTIVE) == BST_CHECKED)
		updateItems.insert(make_pair(UpdateManager::ISP,getText(IDC_ISP_ADDRESS)));
}

void UpdatePage::on(UpdateManagerListener::Types type, int file) throw() {
	prog++;
	int len = GetDlgItem(IDC_STATUS).GetWindowTextLength() + 1;
	AutoArray<TCHAR> buf(len);
	GetDlgItemText(IDC_STATUS, buf, len);
	tstring statText(buf);
	if (type == UpdateManagerListener::COMPLETE) {
		statText += "Completed ";
		statText += Util::getUpdateFileNames(file);
		statText += "(";
		statText += Util::toString(prog);
		statText += " of "; 
		statText += Util::toString(uc);
		statText +=")";
	} else if (type == UpdateManagerListener::FAIL) {
		statText += "Failed on " ;
		statText += Util::getUpdateFileNames(file);
	}

	statText += "\r\n";
	progress.SetPos(prog);
	SetDlgItemText(IDC_STATUS, statText.c_str());
	if(prog == uc){
		::EnableWindow(GetDlgItem(IDC_UPDATE), true);
		::EnableWindow(GetDlgItem(IDC_LOAD_BACKUP), true);
		uncheck(IDC_CLIENT_ACTIVE);
		uncheck(IDC_MYINFO_ACTIVE);
		uncheck(IDC_CLIENTVERSION_ACTIVE);
		uncheck(IDC_ISP_ACTIVE);
	}
}
/**
 * @file
 * $Id: UpdatePage.cpp 303 2005-08-18 23:46:43Z sulan $
 */